// app.js
App({
  async onLaunch() {
    // 登录
    // this.checkLogin(res => {
    //   if(!res.is_login) {
        // const code = await this.login()
        // await this.getToken(code)
        // console.log('拿到token')
    //   }
    // })
    this.globalData.token = wx.getStorageSync('token')
    this.globalData.userInfo = wx.getStorageSync('userInfo')
  },
  login() {
    // 登录
    return new Promise((resolve, reject) => {
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          resolve(res.code)
        }
      })
    })
  },
  getToken(code) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'http://192.168.1.8:5000/wechat_login',
        method: 'post',
        data: {
          code: code
        },
        success: res => {
          this.globalData.token = res.data.data
          resolve(res.data.data)
        }
      })
    })
  },

  globalData: {
    userInfo: null,
    token: null
  }
})
